package org.example.stream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Test01 {
    public static void main(String[] args) {
        //需求：定义一个集合，并且添加1-10的元素，过滤奇数，只留下偶数，并且保存起来
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list,1,2,3,4,5,6,7,8,9,10);
 
        List<Integer> collect = list.stream()
                .filter(new Predicate<Integer>() {
            @Override
            public boolean test(Integer integer) {
                //true就留下
                return integer % 2 == 0;
            }
        }).collect(Collectors.toList());
        //System.out.println(collect);
 
        //用lambda表达式简化开发
        List<Integer> collect1 = list.stream()
                .filter(s -> s % 2 == 0)
                .collect(Collectors.toList());
        System.out.println(collect1);
    }
}